const app = getApp()

const WXAPI = require('../../wxapi/main');
const userSocket = require('../../wxapi/shareSocket');

Page({
  data: {
    loginBIsLoading:"",
    loginBDisable:"",
    joinRoomMd5:"5c5877e1b4cc20bfc51872b87ff3eeba",
    code:""
  },
  getUserInfo:function(){
    var that=this
    // 登录
    wx.login({
     success: res => {
       if (res.code) {
         that.setData({
           code:res.code
         })
         // 查看是否授权
         wx.getSetting({
           success: function (res) {
            console.log(res);
             if (res.authSetting['scope.userInfo']) {
               wx.getUserInfo({
                 success: res => {
                   console.log(res)
                   wx.setStorage({
                     key: 'userInfo',
                     data: res.userInfo,
                   })
                   app.globalData.userInfo = res.userInfo;
                   that.setData({
                    avator:app.globalData.userInfo.avatarUrl,
                    loginBDisable:"true",
                    loginBIsLoading:"",
                    loginBText:"已登录好字帖"
                   })
                   
                   userSocket.linkSocket(2);
                 }
               })
             }else{
             }
           }
         })
       }
     }
    })
  },
  // 用户授权
  bindGetUserInfo: function (e) {
    if(this.data.loginBIsLoading == "true" ||
       this.data.loginBDisable == "true" 
    ){
        return;
    }

    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
        that.setData({
          hasAuthorize:true,
          loginBIsLoading:"true",
          loginBText:"正在登录好字帖",
        });
      
      this.getUserInfo() //查询存储用户信息
    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '还君明珠双泪垂，恨不相逢未嫁时',
        content: 'o(>﹏<)o 不要残忍拒绝我',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了“返回授权”')
          }
        }
      })
    }
  },
  // 用户授权
  loginit: function (e) {
      this.getUserInfo() //查询存储用户信息
  },
  
  onReady:function() {
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '智慧好字帖登录'
    }) 

    if(options.hasOwnProperty('s'))
    {
    }

    var getMd5 = decodeURIComponent(options.scene) 
    if (getMd5 && getMd5!=='undefined') {
      this.setData({
        joinRoomMd5:getMd5
      })     
    }

    this.setData({
      loginBIsLoading:"",
      loginBDisable:"",
      loginBText:"登录好字帖"
     });

     userSocket.getMainThis(this);
  },

  onShow() {
  },
  
  onUnload:function(){
   
  },
  
})